package com.wzl.kgmusic

import android.content.Context
import android.os.Environment
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream

object Utils {
    private const val DIR_RECODER = "recoder"
    private val DIR_IMAGE: String = Environment.DIRECTORY_MUSIC
    private fun getAppDir(): String {
        return BaseApplication.app!!.filesDir.absolutePath
    }

    fun getMusicDir(): String {
        val dir: File = File(getAppDir() + "/" + DIR_IMAGE)
        if (!dir.exists()) dir.mkdirs()
        return dir.absolutePath
    }

    fun getRecoderDir(): String {
        val dir: File = File(getAppDir() + "/" + DIR_RECODER)
        if (!dir.exists()) dir.mkdirs()
        return dir.absolutePath
    }

    fun getRecoderPath(recoderName: String): String {
        return getRecoderDir() + "/" + recoderName
    }
    fun copyFile(context: Context, name: String): String {
        val imageDir: String = getMusicDir()
        val file = File("$imageDir/$name")
        try {
            val `is`: InputStream = context.getAssets().open("十年/${name}")
            val fos = FileOutputStream(file)
            val buffer = ByteArray(4096)
            var len = -1
            while ((`is`.read(buffer).also { len = it }) != -1) {
                fos.write(buffer, 0, len)
            }
            fos.flush()
            fos.close()
            `is`.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return file.absolutePath
    }
}